<?php

class Login extends MX_Controller{
	
	function __construct(){
		parent::__construct();
		//$this->load->helper(array('form','security'));
		//$this->load->library(array('form_validation','session'));
		//$this->form_validation->CI =& $this;
		//$this->output->enable_profiler(TRUE);
	}
	
	
	function index(){

		echo $this->getIp();
		$this->form_validation->set_rules('usuario','Usuario');
		$this->form_validation->set_rules('password','Password','md5|callback_userValid');
		
		$this->form_validation->set_message('userValid', 'Los datos de acceso son incorrectos');
		
		
		$session=$this->session->userdata('id');

		if($this->form_validation->run()==FALSE && empty($session)){
			$this->load->view('login/loginForm');
		}
		else{
			
			if(!isset($_POST['logout'])){	
				$data['datos']=$this->load->model('login/users')->userData($this->session->userdata('id'));
				$this->load->view('login/validUser',$data);
			}
			else{
				$this->logout();
			}
		}

	}
	
	function registro($lang='spanish'){
		$this->load->language('registerForm',$lang);
		$data['lang']=$lang;
		$this->dynamic_assets->add_code_js($this->load->js('registro',$data,true));
		$this->load->view('registerForm');
		//$this->load->view('emaildebug');
	}
	
	function registerComplete($lang){
		
		$this->load->language('errores',$lang);
		
		if(	empty($_POST['nombre']) ||
			empty($_POST['apellidos']) ||
			empty($_POST['dni']) ||
			empty($_POST['email']) ||
			empty($_POST['direccion']) ||
			empty($_POST['ciudad']) ||
			empty($_POST['cp']) ||
			empty($_POST['telefono'])
			){
			echo json_encode(array('error'=>true,'mensaje'=>$this->lang->line('mensaje_vacios')));
			return;
		}
		
		if(!$this->load->model('users')->save_user($this->input->post())){
			echo json_encode(array('error'=>true,'mensaje'=>$this->lang->line('mensaje_email')));
			return;
		}
		
		$config["mailtype"]="html";
		$this->load->library('email',$config);
		$this->email->from('server@oriflame.com', 'Oriflamestar');
		$this->email->to('danidhsm@hotmail.com');
		//$this->email->cc('guillermax89@gmail.com');
		$this->email->bcc(array('dgsoft2012@gmail.com','guillermax89@gmail.com'));
		
		$this->email->subject('Registrazione Oriflame');
		
		$this->load->language('registerForm',$lang);
		$this->email->message($this->load->view('email',array(),true));
		$this->email->send();
		
		echo json_encode(array('error'=>false,'mensaje'=>$this->lang->line('mensaje_enviado')));
		
	}
	function userValid($password){
		
		
		$usuario=$this->form_validation->_field_data['usuario']['postdata'];
		$datosUsuario=$this->load->model('login/users');
		$usuarioValido=$datosUsuario->validUser($usuario,$password);
			
		if(!empty($usuarioValido)){
			
			$this->session->set_userdata(array('id'=>$usuarioValido['user_id']));
			
			return TRUE;
		}
		
		return FALSE;
		
		
	}
	
	function userExist($usuario){

		return $this->load->model('login/users')->userExist($usuario);
	
	}
	
	
	function getIp(){
		if (!empty($_SERVER['HTTP_CLIENT_IP'])){
			$ip=$_SERVER['HTTP_CLIENT_IP'];
		}
		elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
			$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
		}
		else{
			$ip=$_SERVER['REMOTE_ADDR'];
		}
		return $ip;
	}
	
	
	function logout(){
		$this->session->sess_destroy();
		redirect(uri_string());
	}
}